package com.asialjim.wechat.official.apiservice.impl;

import com.asialjim.wechat.core.executor.WeChatApiExecutor;
import com.asialjim.wechat.database.DataBaseService;
import com.asialjim.wechat.official.api.OfficialApi;
import com.asialjim.wechat.official.api.wechatuser.GetWeChatUserApiRequest;
import com.asialjim.wechat.official.api.wechatuser.GetWeChatUserApiResponse;
import com.asialjim.wechat.apiservice.WeChatUserService;
import com.asialjim.wechat.pojo.user.WeChatUser;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import lombok.var;
import java.util.Objects;

/**
 * <h1><em>ASIAL JIM JAVA DOC</em></h1><hr/>
 * <h2>CLASS DESCRIPTION <i>[ NAME: WeChatUserServiceImpl ]</i></h2><strong>
 * <p> we chat user service implements
 * </strong><p><p><em>Copyright &copy; Asial Jim Co., LTD</em><hr/>
 *
 * @author Asial Jim &nbsp;&nbsp; <span>Email:<a href="mailto:asialjim@hotmail.com">asialjim@hotmail.com</a> &nbsp;&nbsp; <a href="asialjim@qq.com">asialjim@qq.com</a></span>
 * @version 1.0.0
 * @since 2021/3/16   &nbsp;&nbsp; JDK 8
 */
@Slf4j
@AllArgsConstructor
public class WeChatUserServiceImpl implements WeChatUserService {
    private final WeChatApiExecutor apiExecutor;
    private final DataBaseService dataBaseService;

    @Override
    public WeChatUser weChatUserInfo(String openId,String subjectId) {
        WeChatUser userExist = dataBaseService.selectOne(WeChatUser.builder().openId(openId).build());
        log.info("\r\n\tGet We Chat User from Data Base by Open Id:[{}], Result:[{}]",openId,userExist);
        GetWeChatUserApiResponse response = apiExecutor.execute(OfficialApi.GET_USER, GetWeChatUserApiRequest.UrlParam.builder().openid(openId).build(), GetWeChatUserApiResponse.class).parse();
        WeChatUser weChatUser = response.weChatUser();
        weChatUser.setSubjectId(subjectId);
        log.info("\r\n\tGet We Chat User Info from Ten-cent Server Result:[{}]",weChatUser);
        if (Objects.isNull(userExist)){
            int insert = dataBaseService.insert(weChatUser);
            log.info("\r\n\tInsert We Chat User:[{}] Result:[{}]",weChatUser,insert);
            return weChatUser;
        }

        if(weChatUser.getSubscribe().equals(userExist.getSubscribe())){
            return weChatUser;
        }

        int update = dataBaseService.updateById(weChatUser);
        log.info("\r\n\tUpdate We Chat User By Open Id:[{}]，\t\n\tTarget: [{}],\r\n\tResult: [{}]",weChatUser.getOpenId(),weChatUser,update);
        return weChatUser;
    }

    @Override
    public WeChatUser weChatUserInfo(String openId) {
        WeChatUser user = WeChatUser.builder().openId(openId).build();
        log.info("\r\n\tGet We Chat User Info by OpenId: [{}], Result: [{}]",openId,user);
        return user;
    }

    @Override
    public void unSubWeChatUser(String openId) {
        var weChatUser = dataBaseService.selectOne(WeChatUser.builder().openId(openId).build());
        if (Objects.isNull(weChatUser)){
            log.warn("\r\n\tWe Chat User[OpenId:{}] Is Not Exist in Data Base",openId);
            return;
        }

        weChatUser.setSubscribe(-1);
        int update = dataBaseService.updateById(weChatUser);
        log.info("\r\n\tWe Chat User[OpenId:{}] Un Subscribe,Update Data Base Result:[{}]",openId,update);
    }
}
